#include "xfb_array.h"

#include <assert.h>

#include "xfb_vector.h"

/* array->items = (void**)array->vec.data; */
static void _xf_array_update(xf_array_t *a)
{
  a->items = xf_vector_obj_data(&a->v, void *);
}

void xf_array_init(xf_array_t *a)
{
  xf_vector_init(&a->v);
  a->items = 0;
  a->sz = 0;
}

void xf_array_destory(xf_array_t *a)
{
  xf_vector_destory(&a->v);
  a->items = 0;
  a->sz = 0;
}

void xf_array_push(xf_array_t *a, void *item)
{
  int ret = xf_vector_obj_push(&a->v, void *, &item);
  assert(ret == 0);
  _xf_array_update(a);
  a->sz++;
}

void *xf_array_pop(xf_array_t *a)
{
  void *item;
  int hr;
  assert(a->sz > 0);
  a->sz--;
  item = a->items[a->sz];
  hr = xf_vector_obj_resize(&a->v, void *, a->sz);
  /* _xf_array_update(a); */
  if (hr) { assert(hr == 0); }
  return item;
}
